# Contributor: nibon7 <nibon7@163.com>
# Maintainer: nibon7 <nibon7@163.com>
pkgname=nushell
pkgver=0.99.1
pkgrel=0
pkgdesc="A new type of shell"
url="https://www.nushell.sh"
# s390x: nix crate
# riscv64: segfault when compiling syn
# loongarch64: mysterious SIGSEGV in a test
arch="all !s390x !riscv64 !loongarch64"
license="MIT"
makedepends="
	cargo
	cargo-auditable
	libgit2-dev
	libssh2-dev
	mimalloc2-dev
	openssl-dev
	sqlite-dev
	"
options="net"
subpackages="$pkgname-doc $pkgname-plugins:_plugins"
install="$pkgname.post-install $pkgname.post-upgrade $pkgname.pre-deinstall"
source="$pkgname-$pkgver.tar.gz::https://github.com/nushell/nushell/archive/$pkgver.tar.gz
	system-deps.patch
	"

export LIBSSH2_SYS_USE_PKG_CONFIG=1  # use system libssh2

# the polars plugin takes too much memory to build on some platforms
case "$CARCH" in
	armhf|armv7|ppc64le|riscv64|x86) _exclude_opts="--exclude nu_plugin_polars" ;;
esac

prepare() {
	default_prepare

	# Rust target triple.
	local target=$(rustc -vV | sed -n 's/host: //p')

	# Build against system-provided libs
	mkdir -p .cargo
	cat >> .cargo/config.toml <<-EOF

		[target.$target]
		git2 = { rustc-link-lib = ["git2"] }
		mimalloc = { rustc-link-lib = ["mimalloc"] }
		rusqlite = { rustc-link-lib = ["sqlite3"] }
	EOF

	cargo fetch --target="$CTARGET" --locked
}

build() {
	cargo auditable build --workspace --release --frozen $_exclude_opts
}

check() {
	case "$CARCH" in
	a*)
		cargo test --workspace --frozen $_exclude_opts -- \
			--skip plugin_persistence::plugin_process_exits_when_nushell_exits \
			--skip plugins::stream::echo_interactivity_on_slow_pipelines
		;;
	*)
		cargo test --workspace --frozen $_exclude_opts
		;;
	esac
}

package() {
	find target/release \
		-maxdepth 1 \
		-executable \
		-type f \
		-name "nu*" \
		-exec install -Dm755 '{}' -t "$pkgdir"/usr/bin/ \;

	install -Dm644 LICENSE \
		-t "$pkgdir"/usr/share/licenses/$pkgname/
}

_plugins() {
	pkgdesc="Nushell plugins"
	depends="nushell=$pkgver-r$pkgrel"

	amove usr/bin/nu_plugin_*
}

sha512sums="
9ea4982aba8dab3cb086af0c27c167b5796bec0edfe8d542e8ddaa4b1d934c53b6524ee2d63ac52ac57241f5baecfbd8f038fa8d0544ee6e5a7c7bbdcc117f27  nushell-0.99.1.tar.gz
275fa114f6cf81fd6dbd63df2e7f5a46afdb8be459693ae2ff7736ec22a9acd2e5c16affc23f35ae757f684ab5075e4793d30b3b28a0c57684fa57bf489b16ed  system-deps.patch
"
